using System;
using System.Threading.Tasks;
using FytSoa.Common;
using FytSoa.Core.Model.TourGuide;
using FytSoa.Service.DtoModel;
using FytSoa.Service.Interfaces;
using FytSoa.Service.Extensions;
using FytSoa.Core.Model.Member;
using SqlSugar;

namespace FytSoa.Service.Implements.TourGuide
{
    public class TourGuideOrderService : BaseService<TourGuideOrder>, ITourGuideOrderService
    {
        /// <summary>
        /// 根据条件查询订单服务
        /// </summary>
        public async Task<ApiResult<Page<TourGuideOrder>>> GetPageWhereList(PageParm param)
        {
            var result = new ApiResult<Page<TourGuideOrder>>() { statusCode = (int)ApiEnum.Error };
            try
            {
                string beginTime = string.Empty, endTime = string.Empty;
                if (!string.IsNullOrEmpty(param.time))
                {
                    var timeRes = Utils.SplitString(param.time, '-');
                    beginTime = timeRes[0].Trim();
                    endTime = timeRes[1].Trim();
                }
                result.data = await Db.Queryable<TourGuideOrder, Member>((m, u) => new JoinQueryInfos(
                    JoinType.Inner, m.MemberGuid == u.Guid
                ))
                .WhereIF(!string.IsNullOrEmpty(param.number) && param.number == "base", (m, u) => m.Mold == 1 || m.Mold == 2)
                .WhereIF(!string.IsNullOrEmpty(param.number) && param.number == "vip", (m, u) => m.Mold == 3 || m.Mold == 4)
                .WhereIF(!string.IsNullOrEmpty(param.number) && param.number == "gate", (m, u) => m.Mold == 5 || m.Mold == 6)
                .WhereIF(!string.IsNullOrEmpty(param.number) && param.number == "pass", (m, u) => m.Mold == 7)
                .WhereIF(!string.IsNullOrEmpty(param.number) && param.number == "lounge", (m, u) => m.Mold == 8)
                .WhereIF(!string.IsNullOrEmpty(param.guid), (m, u) =>m.MemberGuid==param.guid) //根据用户查询
                .WhereIF(param.id==1, (m, u) => m.Mold==2 || m.Mold==4 || m.Mold==6) //送站
                .WhereIF(param.id == 2, (m, u) => m.Mold == 1 || m.Mold == 3 || m.Mold == 5 || m.Mold == 7 || m.Mold == 8) //送站
                .WhereIF(param.types != 0, (m, u) => m.Types == param.types)  //国内国际
                .WhereIF(!string.IsNullOrEmpty(param.key), (m, u) =>m.Number.Contains(param.key) || m.FlightNumber.Contains(param.key) || m.DriverPhone.Contains(param.key) || u.Mobile.Contains(param.key))
                .WhereIF(!string.IsNullOrEmpty(param.time), (m, u) => m.ServiceData >= Convert.ToDateTime(beginTime) && m.ServiceData <= Convert.ToDateTime(endTime))
                .WhereIF(param.attr != 0, (m, u) => m.Mold == param.attr)  // 8个状态
                .WhereIF(param.status != -1, (m, u) => m.ServiceStatus == param.status) //服务状态
                .OrderBy((m, u) => m.AddTime, SqlSugar.OrderByType.Desc)
                .Select((m, u) => m)
                .Mapper(it => it.Member, it => it.MemberGuid)
                .ToPageAsync(param.page, param.limit);
                result.statusCode = (int)ApiEnum.Status;
            }
            catch (System.Exception ex)
            {
                result.message = ex.Message;
            }
            return result;
        }
    }
}
